home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / KB / KB_7300.C next >
C/C++ Source or Header  |  1991-11-04  |  13KB  |  622 lines

  1. /*************************************************************************
  2.  
  3.     kb_7300.c       Keyboard (KB) Subroutines
  4.             for Bywater Software
  5.  
  6.             Implementation for AT&T Unix PC (7300)
  7.  
  8.             Copyright (c) 1991, Ted A. Campbell
  9.  
  10.             Bywater Software
  11.             P. O. Box 4023 
  12.             Duke Station 
  13.             Durham, NC  27706
  14.  
  15.             email: tcamp@hercules.acpub.duke.edu
  16.  
  17.     Copyright and Permissions Information:
  18.  
  19.     All U.S. and international copyrights are claimed by the
  20.     author. The author grants permission to use this code
  21.     and software based on it under the following conditions:
  22.     (a) in general, the code and software based upon it may be 
  23.     used by individuals and by non-profit organizations; (b) it
  24.     may also be utilized by governmental agencies in any country,
  25.     with the exception of military agencies; (c) the code and/or
  26.     software based upon it may not be sold for a profit without
  27.     an explicit and specific permission from the author, except
  28.     that a minimal fee may be charged for media on which it is
  29.     copied, and for copying and handling; (d) the code must be 
  30.     distributed in the form in which it has been released by the
  31.     author; and (e) the code and software based upon it may not 
  32.     be used for illegal activities. 
  33.  
  34. **************************************************************************/
  35.  
  36. #define    ATT_TAM        TRUE
  37. #define    ATT_MGR        FALSE
  38. #define    WRITE_OUTPUT    FALSE
  39.  
  40. #include "stdio.h"
  41. #include "fcntl.h"
  42. #include "signal.h"
  43. #include "sys/stat.h"
  44. #include "termio.h"
  45. #include "bw.h"
  46. #include "kb.h"
  47.  
  48. #if    ATT_TAM
  49. #include "tam.h"
  50. #define    NO_KEY    -1
  51. #else
  52. #define    NO_KEY    FALSE
  53. #endif
  54.  
  55. #ifndef    TRUE
  56. #define    TRUE    1
  57. #define    FALSE    0
  58. #endif
  59.  
  60. #ifndef    ESC
  61. #define    ESC    0x1b
  62. #endif
  63.  
  64. #if ATT_TAM
  65. extern int w;            /* window identifier */
  66. extern int tam_mready;        /* is mouse ready with data ? */
  67. extern int tam_mx, tam_my;    /* mouse position x, y */
  68. int tam_mb, tam_mr;        /* button, reason for mouse */
  69. int tam_kready = FALSE;
  70. int kb_hold = 0;
  71.  
  72. #if WRITE_OUTPUT
  73. FILE *outfile;
  74. #endif
  75.  
  76. #else
  77. struct termio old_ttystruct;
  78. int    kb_tty;            /* fd for new terminal file */
  79. #endif
  80.  
  81.  
  82. #ifdef    KB_TEST
  83. main()
  84.    {
  85.    register int s;
  86.    static char tbuf[ 3 ];
  87.  
  88.    kb_init();
  89.  
  90.    tbuf[ 0 ] = 0;
  91.    while ( tbuf[ 0 ] != 0x1b )
  92.       {
  93. #if ATT_TAM
  94.       s = wgetc( w );
  95. #else
  96.       s = read( kb_tty, tbuf, 1 );
  97. #endif
  98.       printf( "The read() function returned %d \n", s );
  99.       if ( s > 0 )
  100.          {
  101.          printf( "The key is <%c> \n", tbuf[ 0 ] );
  102.          }
  103.       }
  104.  
  105.    kb_deinit();
  106.    }
  107. #endif
  108.  
  109. /*************************************************************************
  110.  
  111.    FUNCTION:       kb_init()
  112.  
  113.    DESCRIPTION:    This function should perform any initialization
  114.          necessary for the keyboard system.
  115.  
  116.    INPUT:          none.
  117.  
  118.    RETURNS:        none.
  119.  
  120. **************************************************************************/
  121.  
  122. kb_init()
  123.    {
  124. #if  ATT_TAM
  125.  
  126.    keypad( 0, 1 );
  127.    wndelay( w, 1 );
  128.  
  129. #if WRITE_OUTPUT
  130.    if ( ( outfile = fopen( "output", "w" ) ) == NULL )
  131.       {
  132.       bw_error( "Failed to open outfile <output> " );
  133.       }
  134. #endif    
  135. #else
  136.    struct termio new_ttystruct;
  137.  
  138.    if ( ioctl( 0, TCGETA, &old_ttystruct ) < 0 )  
  139.       {
  140.       bw_error( "Failed to get old tty parameters." );
  141.       }
  142.  
  143.    if ( ( kb_tty = open( "/dev/tty", O_NDELAY)) == -1 )
  144.       {
  145.       bw_error( "Failed to open /dev/tty" );
  146.       }
  147. #ifdef   DEBUG
  148.    else
  149.       {
  150.       sprintf( bw_ebuf, "/dev/tty returns %d \n", kb_tty );
  151.       bw_debug( bw_ebuf );
  152.       }
  153. #endif
  154.  
  155.    if ( ioctl( kb_tty, TCGETA, &new_ttystruct ) < 0 )  
  156.       {
  157.       bw_error( "Failed to get new tty parameters." );
  158.       }
  159.  
  160.    new_ttystruct.c_cc[4] = 0;      /* minimum characters */
  161.    new_ttystruct.c_cc[5] = 0;      /* maximum time */
  162.    new_ttystruct.c_iflag = 0;
  163.    new_ttystruct.c_lflag = ISIG;
  164.    new_ttystruct.c_cflag &= ~CSIZE;
  165.    new_ttystruct.c_cflag |= CS8;
  166.    new_ttystruct.c_cflag &= ~PARENB;
  167.    if ( ioctl( kb_tty, TCSETA, &new_ttystruct ) < 0 )  
  168.       {
  169.       fprintf( stderr, "Failed to set new tty parameters. \n" );
  170.       }
  171.  
  172. #endif
  173.  
  174.    }
  175.  
  176.  
  177.  
  178. /*************************************************************************
  179.  
  180.    FUNCTION:       kb_deinit()
  181.  
  182.    DESCRIPTION:    This function should perform any necessary
  183.          deinitialization, that is, return the keyboard
  184.          to its default state when a Simple Software
  185.          program is to be exited.
  186.  
  187.    INPUT:          none.
  188.  
  189.    RETURNS:        none.
  190.  
  191. **************************************************************************/
  192.  
  193. kb_deinit()
  194.    {
  195. #if  ATT_TAM
  196. #else
  197.    if ( ioctl( 0, TCSETA, &old_ttystruct ) < 0 )
  198.       {
  199.       fprintf( stderr, "Failed to restore old tty parameters. \n");
  200.       }
  201. #endif
  202.    }
  203.  
  204. /*************************************************************************
  205.  
  206.    FUNCTION:       kb_rxstat()
  207.  
  208.    DESCRIPTION:    This function determines whether a character is
  209.            ready from the console.  The function is used
  210.            especially in telecommunications programs,
  211.            where it is necessary to poll the keyboard
  212.            without locking up the program waiting for a
  213.            response.
  214.  
  215.    INPUT:          none.
  216.  
  217.    RETURNS:        The function returns 0 if no character is
  218.            available and 1 if a character is available.
  219.  
  220. **************************************************************************/
  221.  
  222. kb_rxstat()
  223.    {
  224.  
  225. #if WRITE_OUTPUT
  226.    fprintf( outfile, "kb_rxstat(): enter\n" );
  227.    fflush( outfile );
  228. #endif
  229.  
  230.    if ( tam_kready == FALSE )
  231.       {
  232.       att_rxstat();
  233.       }
  234.  
  235. #if WRITE_OUTPUT
  236.    fprintf( outfile, "kb_rxstat(): returns %d\n", tam_kready );
  237.    fflush( outfile );
  238. #endif
  239.  
  240.    return tam_kready;
  241.    }
  242.  
  243. att_rxstat()
  244.    {
  245.    int r;
  246.    register int s;
  247. #if ATT_TAM
  248. #else
  249.    static char tbuf[3];
  250. #endif
  251.  
  252. #if WRITE_OUTPUT
  253.    fprintf( outfile, "att_rxstat(): enter\n" );
  254.    fflush( outfile );
  255. #endif
  256.  
  257.    /* This should be the only point at which a character
  258.       is read */
  259.  
  260. #if ATT_TAM
  261.    s = wgetc( w );
  262. #else
  263.    s = read( kb_tty, tbuf, 1 );
  264. #endif
  265.  
  266.    /* if no character is available, return false */
  267.  
  268.    if ( s == NO_KEY )
  269.       {
  270.       return FALSE;
  271.       }
  272.  
  273.    /* we have now read a character */
  274.  
  275. #if  ATT_TAM
  276.    kb_hold = s;
  277. #else
  278.    kb_hold = tbuf[ 0 ];
  279. #endif
  280.  
  281. #if WRITE_OUTPUT
  282.    fprintf( outfile, "att_rxstat(): read kb_hold 0x%02x\n", kb_hold );
  283.    fflush( outfile );
  284. #endif
  285.  
  286.    /* check to see if the available key indicates
  287.       an escape sequence of any sort */
  288.  
  289.    if ( kb_hold == ESC )
  290.       {
  291.       r = att_esc();            
  292.       }
  293.    else if ( kb_hold > 127 )
  294.       {
  295.       r = att_seq( kb_hold );
  296.       }
  297.    else
  298.       {
  299.       r = kb_hold;
  300.       }
  301.  
  302. #if WRITE_OUTPUT
  303.    fprintf( outfile, "att_rxstat(): read r 0x%02x\n", r );
  304.    fflush( outfile );
  305. #endif
  306.  
  307.    /* now check to see if there is a key or mouse input
  308.       available */
  309.  
  310.    if ( r == 0 )
  311.       {
  312.       if ( ( tam_kready == TRUE ) || ( tam_mready == TRUE ))
  313.          {
  314.          return TRUE;
  315.          }
  316.       else
  317.          {
  318.          return FALSE;
  319.          }
  320.       }
  321.  
  322.    /* the available key is not 0; thus a character is available
  323.       as "r" */
  324.  
  325.    kb_hold = r;
  326.    tam_kready = TRUE;
  327.    return TRUE;
  328.    }
  329.  
  330. /*************************************************************************
  331.  
  332.    FUNCTION:       kb_rx()
  333.  
  334.    DESCRIPTION:    This function returns a single character from
  335.            the keyboard.  If a character is not available
  336.            it waits.  The function should be able to
  337.            recognize any special keys, and return the
  338.            appropriate Simple Software KB conventions
  339.            designated for them.
  340.  
  341.    INPUT:          none.
  342.  
  343.    RETURNS:        The function returns the ASCII code for the
  344.            key pressed, or the Simple Software KB convention
  345.            (see kb.h) for a function or other special key.
  346.  
  347. **************************************************************************/
  348.  
  349. kb_rx()
  350.    {
  351. #if WRITE_OUTPUT
  352.    int r;
  353.    
  354.    r = att_rx();
  355.    fprintf( outfile, "kb_rx(): 0x%02x\n", r );
  356.    fflush( outfile );
  357.    return r;
  358. #else
  359.    return att_rx();
  360. #endif
  361.    }
  362.  
  363. att_rx()
  364.    {
  365.  
  366.    while( tam_kready == FALSE )
  367.       {
  368.       att_rxstat();
  369.       }
  370.  
  371.    tam_kready = FALSE;
  372.    return kb_hold;
  373.    }
  374.  
  375. att_esc()
  376.    {
  377.    register int c;
  378.    static char sequence[ 3 ];
  379.  
  380. #if WRITE_OUTPUT
  381.    fprintf( outfile, "att_esc(): \n" );
  382.    fflush( outfile );
  383. #endif
  384.  
  385.    if ( kb_rxstat() == TRUE )
  386.       {
  387.       sequence[ 0 ] = kb_hold;
  388.       sequence[ 1 ] = 0;
  389.       tam_kready = 0;
  390.       }
  391.    else
  392.       {
  393.       return ESC;
  394.       }
  395.  
  396.    if ( kb_rxstat() == TRUE )
  397.       {
  398.       sequence[ 1 ] = kb_hold;
  399.       sequence[ 2 ] = 0;
  400.       tam_kready = FALSE;
  401.       }
  402.  
  403.    if ( strcmp( sequence, "[A" ) == 0 )
  404.       {
  405.       return KB_UP;
  406.       }
  407.    else if ( strcmp( sequence, "[B" ) == 0 )
  408.       {
  409.       return KB_DOWN;
  410.       }
  411.    else if ( strcmp( sequence, "[D" ) == 0 )
  412.       {
  413.       return KB_LEFT;
  414.       }
  415.    else if ( strcmp( sequence, "[C" ) == 0 )
  416.       {
  417.       return KB_RIGHT;
  418.       }
  419.    else if ( strcmp( sequence, "[?" ) == 0 )
  420.       {
  421.       tam_mready = TRUE;
  422.       wreadmouse( w, &tam_mx, &tam_my, &tam_mb, &tam_mr );
  423.       return 0;
  424.       }
  425.    else if ( strcmp( sequence, "Nh" ) == 0 )
  426.       {
  427.       return KB_P_DOWN;
  428.       }
  429.    else if ( strcmp( sequence, "Ng" ) == 0 )
  430.       {
  431.       return KB_P_UP;
  432.       }
  433.    else if ( strcmp( sequence, "Nf" ) == 0 )
  434.       {
  435.       return KB_DELETE;
  436.       }
  437.    else if ( strcmp( sequence, "Nj" ) == 0 )
  438.       {
  439.       return KB_INSERT;
  440.       }
  441. /*
  442.    else if ( strcmp( sequence, "Oc" ) == 0 )
  443.       {
  444.       return KB_HELP;
  445.       }
  446.    else if ( strcmp( sequence, "NK" ) == 0 )
  447.       {
  448.       return KB_W_LEFT;
  449.       }
  450.    else if ( strcmp( sequence, "NL" ) == 0 )
  451.       {
  452.       return KB_W_RIGHT;
  453.       }
  454. */
  455.    else if ( strcmp( sequence, "[H" ) == 0 )
  456.       {
  457.       return KB_HOME;
  458.       }
  459.    else if ( strcmp( sequence, "NM" ) == 0 )
  460.       {
  461.       return KB_HOME;
  462.       }
  463.    else if ( strcmp( sequence, "NN" ) == 0 )
  464.       {
  465.       return KB_END;
  466.       }
  467.    else if ( strcmp( sequence, "O" ) == 0 )
  468.       {
  469.       return KB_END;
  470.       }
  471. /*
  472.    else if ( strcmp( sequence, "Ne" ) == 0 )
  473.       {
  474.       return KB_D_WORD;
  475.       }
  476.    else if ( strcmp( sequence, "NE" ) == 0 )
  477.       {
  478.       return KB_D_LINE;
  479.       }
  480.    else if ( strcmp( sequence, "Ox" ) == 0 )
  481.       {
  482.       return KB_FIND;
  483.       }
  484.    else if ( strcmp( sequence, "Oy" ) == 0 )
  485.       {
  486.       return KB_REPLACE;
  487.       }
  488.    else if ( strcmp( sequence, "Ow" ) == 0 )
  489.       {
  490.       return KB_ABANDON;
  491.       }
  492.    else if ( strcmp( sequence, "Oo" ) == 0 )
  493.       {
  494.       return KB_SAVE;
  495.       }
  496.    else if ( strcmp( sequence, "Ok" ) == 0 )
  497.       {
  498.       return KB_QUIT;
  499.       }
  500. */
  501.    else
  502.       {
  503.       return 0;
  504.       }
  505.    }
  506.  
  507. att_seq( k )
  508.    int k;
  509.    {
  510.    char *map;
  511.  
  512.    map = kcodemap( k );
  513.  
  514. #if WRITE_OUTPUT
  515.    fprintf( outfile, "att_seq(): char 0x%02x, map <%s>\n", k, map + 1 );
  516.    fflush( outfile );
  517. #endif
  518.  
  519.    if ( strncmp( map + 1, "[A", 2 ) == 0 )
  520.       {
  521.       return KB_UP;
  522.       }
  523.    else if ( strncmp( map + 1, "[B", 2 ) == 0 )
  524.       {
  525.       return KB_DOWN;
  526.       }
  527.    else if ( strncmp( map + 1, "[D", 2 ) == 0 )
  528.       {
  529.       return KB_LEFT;
  530.       }
  531.    else if ( strncmp( map + 1, "[C", 2 ) == 0 )
  532.       {
  533.       return KB_RIGHT;
  534.       }
  535.    else if ( strncmp( map + 1, "[?", 2 ) == 0 )
  536.       {
  537.       tam_mready = TRUE;
  538.       wreadmouse( w, &tam_mx, &tam_my, &tam_mb, &tam_mr );
  539.       return 0;
  540.       }
  541.    else if ( strncmp( map + 1, "Nh", 2 ) == 0 )
  542.       {
  543.       return KB_P_DOWN;
  544.       }
  545.    else if ( strncmp( map + 1, "Ng", 2 ) == 0 )
  546.       {
  547.       return KB_P_UP;
  548.       }
  549.    else if ( strncmp( map + 1, "Nf", 2 ) == 0 )
  550.       {
  551.       return KB_DELETE;
  552.       }
  553.    else if ( strncmp( map + 1, "Nj", 2 ) == 0 )
  554.       {
  555.       return KB_INSERT;
  556.       }
  557. /*
  558.    else if ( strncmp( map + 1, "Oc", 2 ) == 0 )
  559.       {
  560.       return KB_HELP;
  561.       }
  562.    else if ( strncmp( map + 1, "NK", 2 ) == 0 )
  563.       {
  564.       return KB_W_LEFT;
  565.       }
  566.    else if ( strncmp( map + 1, "NL", 2 ) == 0 )
  567.       {
  568.       return KB_W_RIGHT;
  569.       }
  570. */
  571.    else if ( strncmp( map + 1, "[H", 2 ) == 0 )
  572.       {
  573.       return KB_HOME;
  574.       }
  575.    else if ( strncmp( map + 1, "NM", 2 ) == 0 )
  576.       {
  577.       return KB_HOME;
  578.       }
  579.    else if ( strncmp( map + 1, "NN", 2 ) == 0 )
  580.       {
  581.       return KB_END;
  582.       }
  583.    else if ( strncmp( map + 1, "O", 2 ) == 0 )
  584.       {
  585.       return KB_END;
  586.       }
  587. /*
  588.    else if ( strncmp( map + 1, "Ne", 2 ) == 0 )
  589.       {
  590.       return KB_D_WORD;
  591.       }
  592.    else if ( strncmp( map + 1, "NE", 2 ) == 0 )
  593.       {
  594.       return KB_D_LINE;
  595.       }
  596.    else if ( strncmp( map + 1, "Ox", 2 ) == 0 )
  597.       {
  598.       return KB_FIND;
  599.       }
  600.    else if ( strncmp( map + 1, "Oy", 2 ) == 0 )
  601.       {
  602.       return KB_REPLACE;
  603.       }
  604.    else if ( strncmp( map + 1, "Ow", 2 ) == 0 )
  605.       {
  606.       return KB_ABANDON;
  607.       }
  608.    else if ( strncmp( map + 1, "Oo", 2 ) == 0 )
  609.       {
  610.       return KB_SAVE;
  611.       }
  612.    else if ( strncmp( map + 1, "Ok", 2 ) == 0 )        
  613.       {
  614.       return KB_QUIT;
  615.       }
  616. */
  617.    else
  618.       {
  619.       return 0;
  620.       }
  621.    }
  622.